﻿(async () => {
    await window.ris.initialize();

    $(() => {
        const $eventEmitter = $('body > .container');

        let reloadGrid = null;
        let exportParams = null;
        let exportData = (func) => {
            if (func instanceof Function) {
                func(exportParams);
            }
        };
        $('.main').splitter({
            orientation: 'v',
            panel1Size: '300px',
        });
        const quickFilter = $('.quick-filter').quickFilter(
            `/api/exam-statistics/get-statistics`,
            filterId => {
                $.invokeAction(
                    '',
                    `/api/exam-statistics/get-advanced-search-view`,
                    { filterId }
                ).then(result => {
                    if (result.success) {
                        reloadGrid = () => {
                            dataGrid.reload({ userData: result.data });
                        }
                        reloadGrid();
                    }
                });
            },
            {
                type: 'GET',
            }
        );
        const dataGrid = $('.exam-grid').examGrid({
            columnAutoWidth: true,
            //不分页
            noKey: true,
            remoteOperations: false,
            paging: { enabled: false },
            pager: { visible: false },
            onLoad(loadOptions) {
                return new Promise((resolve, reject) => {
                    if (loadOptions.userData && !$.isEmptyObject(loadOptions.userData)) {
                        exportParams = loadOptions;
                        $.post({
                            dataType: 'json',
                            contentType: 'application/json;charset=UTF-8',
                            url: `/api/exam-statistics/get-list`,
                            data: JSON.stringify(loadOptions),
                            success: result => {
                                if (result.success) {
                                    resolve(result)
                                } else {
                                    reject(result.error);
                                }
                            }
                        });
                    } else {
                        resolve({ success: true, data: { rows: [], totalCount: 0 } })
                    }
                })
            }
        });

        const toolbar = $('.toolbar').dxToolbar({}).dxToolbar('instance');
        const tools = $.getStatisticTools($eventEmitter, quickFilter, dataGrid, {
            reloadGrid: () => {
                if (reloadGrid instanceof Function) {
                    reloadGrid();
                }
            },
            exportData: (func) => {
                if (exportData instanceof Function) {
                    exportData(func);
                }
            }
        });
        toolbar.option('items', [tools.refresh, tools.export]);
    });
})();
